package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.base.domain.ProcedureTeamRel;
import com.ruoyi.base.domain.TeamManagement;
import com.ruoyi.base.mapper.ProcedureTeamRelMapper;
import com.ruoyi.base.mapper.TeamManagementMapper;
import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.base.vo.WorkProcedureVO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.WorkingProcedureMapper;
import com.ruoyi.base.domain.WorkingProcedure;
import com.ruoyi.base.service.IWorkingProcedureService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工序Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-17
 */
@Service
public class WorkingProcedureServiceImpl implements IWorkingProcedureService 
{
    @Autowired
    private WorkingProcedureMapper workingProcedureMapper;

    @Autowired
    private ProcedureTeamRelMapper procedureTeamRelMapper;
    @Autowired
    private TeamManagementMapper teamManagementMapper;

    /**
     * 查询工序
     * 
     * @param id 工序主键
     * @return 工序
     */
    @Override
    public WorkProcedureVO selectWorkingProcedureById(Long id) {
        WorkingProcedure workingProcedure = workingProcedureMapper.selectWorkingProcedureById(id);
        List<WorkProcedureVO> workProcedureVOS = workingProcedureMapper.selectWorkingProcedureList(workingProcedure);
        return workProcedureVOS.get(0);
    }

    /**
     * 查询工序列表
     * 
     * @param workingProcedure 工序
     * @return 工序
     */
    @Override
    public List<WorkProcedureVO> selectWorkingProcedureList(WorkingProcedure workingProcedure) {
        return workingProcedureMapper.selectWorkingProcedureList(workingProcedure);
    }

    /**
     * 新增工序
     * 
     * @param workProcedureVO 工序
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWorkingProcedure(WorkProcedureVO workProcedureVO) {
        WorkingProcedure workingProcedure = new WorkingProcedure();

        BeanUtils.copyProperties(workProcedureVO,workingProcedure);

        workingProcedure.setCreateTime(DateUtils.getNowDate());
        workingProcedure.setProcedureNo(GenCodeUtils.getProcedureNo());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        workingProcedure.setCreateUser(user.getUserName());
        workingProcedure.setCreateRole(user.getRoles().get(0).getRoleName());

        //添加 工序表-班组表-关联表
        for (String teamNo : workProcedureVO.getTeamNoList()) {
            ProcedureTeamRel procedureTeamRel = new ProcedureTeamRel();
            procedureTeamRel.setTeamNo(teamNo);
            procedureTeamRel.setProcedureNo(workingProcedure.getProcedureNo());
            procedureTeamRelMapper.insertProcedureTeamRel(procedureTeamRel);
        }
        return workingProcedureMapper.insertWorkingProcedure(workingProcedure);
    }

    /**
     * 修改工序
     * 
     * @param workingProcedure 工序
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWorkingProcedure(WorkProcedureVO workProcedureVO) {
        //解除 工序表-班组表 对应关系
        //执行删除删除
        String[] procedureNoS = {workProcedureVO.getProcedureNo()};
        procedureTeamRelMapper.deleteProcedureTeamRelByProcedureNo(procedureNoS);

        //添加 工序表-班组表-关联表
        for (String teamNo : workProcedureVO.getTeamNoList()) {
            ProcedureTeamRel procedureTeamRel = new ProcedureTeamRel();
            procedureTeamRel.setTeamNo(teamNo);
            procedureTeamRel.setProcedureNo(workProcedureVO.getProcedureNo());
            procedureTeamRelMapper.insertProcedureTeamRel(procedureTeamRel);
        }
        WorkingProcedure workingProcedure = new WorkingProcedure();
        BeanUtils.copyProperties(workProcedureVO,workingProcedure);

        return workingProcedureMapper.updateWorkingProcedure(workingProcedure);
    }

    /**
     * 批量删除工序
     * 
     * @param ids 需要删除的工序主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWorkingProcedureByIds(Long[] ids) {
        for (Long id : ids) {
            WorkingProcedure workingProcedure = workingProcedureMapper.selectWorkingProcedureById(id);
            String[] procedureNoS = {workingProcedure.getProcedureNo()};
            //删除工序表-班组表-关联表
            int i = procedureTeamRelMapper.deleteProcedureTeamRelByProcedureNo(procedureNoS);
        }
        return workingProcedureMapper.deleteWorkingProcedureByIds(ids);
    }

    /**
     * 删除工序信息
     * 
     * @param id 工序主键
     * @return 结果
     */
    @Override
    public int deleteWorkingProcedureById(Long id)
    {
        return workingProcedureMapper.deleteWorkingProcedureById(id);
    }
}
